package com.cacho.s2b.lesson.engine;

import com.cacho.s2b.lesson.vo.OperationType;
import com.cacho.s2b.lesson.vo.ValueOperation;
import com.cacho.s2b.lesson.vo.ValueStore;
import com.cacho.s2b.lesson.vo.ValueStores;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description 接口参数分析和处理(路径处理 ， 参数增删改)
 * @Author LingWei
 * @date 2023/12/31
 **/
@Setter
@Getter
@Slf4j
public class ValueParser {
    private ApiExpressEngine express;
    private ValueOperation valueOperation;
    private String valueExpression; // 需要处理的数据表达式，可以是json

    public ValueParser(ApiExpressEngine express, String valueExpression) {
        this.express = express;
        this.valueExpression = valueExpression;
    }

    public void parse() {
        valueOperation = new ValueOperation();
        ValueStores valueStores = new ValueStores();
        valueOperation.setOperationType(OperationType.UPDATE); // 设置默认为 update
        valueOperation.setValueStores(valueStores);

        ValueStore[] vs = new ValueStore[]{new ValueStore()}; // 赋空数组

        valueExpression = valueExpression.trim(); // 空格处理
        String gramExpress = "";
        if (valueExpression.equalsIgnoreCase("{{deleted}}") ||
                valueExpression.equalsIgnoreCase("{{delete}}")) {
            valueOperation.setOperationType(OperationType.DELETE);
        }
        // {{add,temp2}}
        else if (valueExpression.toLowerCase().startsWith("{{add") && valueExpression.endsWith("}}")) {
            valueOperation.setOperationType(OperationType.ADD);
            gramExpress = valueExpression.substring("{{add".length(),
                            valueExpression.length() - "}}".length())
                    .trim().substring(1);
            log.info("处理add后：{}", gramExpress);
            valueStores = parseToValueStores(express, gramExpress);
        }
        // {{put,Purpose,test}}
        else if (valueExpression.toLowerCase().startsWith("{{put") && valueExpression.endsWith("}}")) {
            valueOperation.setOperationType(OperationType.PUT);
            gramExpress = valueExpression.substring("{{put".length(),
                            valueExpression.length() - "}}".length())
                    .trim().substring(1); // Purpose,test
            log.info("处理put后：{}", gramExpress);
            valueOperation.setKey(gramExpress.split(",", 2)[0].trim()); // 分割得到2个字符串
            gramExpress = gramExpress.split(",", 2)[1]; //
            valueStores = parseToValueStores(express, gramExpress);
        } else {
            gramExpress = valueExpression;
            valueStores = parseToValueStores(express, gramExpress);
        }
        log.info("需要处理的json 值：{}", gramExpress);
        valueOperation.setValueStores(valueStores);
    }

    // 处理 json 是 list 格式的情况 [a,b,c]
    private ValueStores parseToValueStores(ApiExpressEngine express, String gramExpress) {
        ValueStore[] vs = new ValueStore[1];  // 防止不是 [] 的情况
        ValueStores valueStores = new ValueStores();
        valueStores.setValueStores(vs);
        valueStores.setArray(false); // 默认不是数组
        gramExpress = StringUtils.isEmpty(gramExpress) ? gramExpress : gramExpress.trim();
        if (gramExpress.startsWith("[") && gramExpress.endsWith("]")) {
            valueStores.setArray(true); // 数组处理 []
            String arrayStr = gramExpress.substring(1, gramExpress.length() - 1).trim(); // 去除 []
            if (!"".equals(arrayStr)) {
                String[] parts = arrayStr.split(","); // 按，拆分成数组值
                vs = new ValueStore[parts.length];
                for (int i = 0; i < parts.length; i++) {
                    vs[i] = new ValueStore();
                    vs[i].setValue(express.translate(parts[i]).toString());
                    vs[i].setNeedQuote(express.isUseQuote());
                    valueStores.setValueStores(vs);
                }
            } // 不为空时
            else {
                valueStores.setValueStores(new ValueStore[]{});
            }
        } // 不是 [] 这种类型
        else {
            vs[0] = new ValueStore(express.translate(gramExpress).toString(), express.isUseQuote());
        }
        return valueStores;
    }
}
